package com.gimi.cloud.bbp.task.service.impl;

import com.gimi.cloud.bbp.dto.task.ScheduleJobDTO;
import com.gimi.cloud.bbp.task.service.ScheduleJobService;
import com.gimi.cloud.bbp.task.util.ScheduleUtils;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 定时任务service实现类
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-08
 **/
@Service
public class ScheduleJobServiceImpl implements ScheduleJobService {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleJobServiceImpl.class);

    private Scheduler scheduler;

    @Autowired
    public ScheduleJobServiceImpl(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    @Override
    public ScheduleJobDTO getScheduleJobByJobKey(ScheduleJobDTO dto, String jobKey) throws Exception {
        return null;
    }

    @Override
    public void createScheduleJob(ScheduleJobDTO scheduleJob) {
        if(null == scheduleJob.getJobId()){
            logger.info("新增定时任务失败，id为空");
        }
        ScheduleUtils.createScheduleJob(scheduler,scheduleJob);
    }

    @Override
    public void updateScheduleJob(ScheduleJobDTO scheduleJob) {
        if(null == scheduleJob.getJobId()){
            logger.info("更新定时任务失败，id为空");
        }
        ScheduleUtils.updateScheduleJob(scheduler,scheduleJob);
    }

    @Override
    public void deleteScheduleJob(ScheduleJobDTO scheduleJob) {
        //删除时，先判断定时任务是否存在
        if(null == scheduleJob.getJobId()){
            logger.info("删除定时任务失败，id为空");
        }
        ScheduleUtils.deleteScheduleJob(scheduler,scheduleJob.getJobId());
    }

    @Override
    public void runScheduleJob(ScheduleJobDTO scheduleJob) {
        if(null == scheduleJob.getJobId()){
            logger.info("执行定时任务失败，id为空");
        }
        ScheduleUtils.run(scheduler,scheduleJob);
    }

    @Override
    public void pauseScheduleJob(ScheduleJobDTO scheduleJob) {
        if(null == scheduleJob.getJobId()){
            logger.info("暂停定时任务失败，id为空");
        }
        ScheduleUtils.pauseJob(scheduler,scheduleJob.getJobId());
    }

    @Override
    public void resumeScheduleJob(ScheduleJobDTO scheduleJob) {
        if(null == scheduleJob.getJobId()){
            logger.info("恢复定时任务失败，id为空");
        }
        ScheduleUtils.resumeJob(scheduler,scheduleJob.getJobId());
    }
}
